#include <curses.h>
#include <menu.h>

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD   4

char *choices[] = {
                        "New Game",
                        "Load Game",
                        "Options",
                        "Credits",
                        "Exit"
                  };

char *numbers[] = {
			"1. ",
			"2. ",
			"3. ",
			"4. ",
			"5. "
		};
char *screen[] = {
	"................................................................................",
	"................................................................................",
	"................................................................................",
	"................................................................................",
	"................................................................................",
	"................................................................................",
	"................................................................................",
	"................................................................................",
	"................................................................................",
	"................................................................................",
	"................................................................................",
	"................................................................................",
	".............................................................+==..==+...........",
	".............................................................||....||...........",
	".............................................................||....||...........",
	".............................................................||____||...........",
	".............................................................+======+...........",	
	"................................................................................",
	"................................................................................",
	"................................................................................",
	"................................................................................",
	"................................................................................",
	"................................................................................",
	"................................................................................"
};

int game(int scr_x, int scr_y);
int do_touch(char moveto);


int main()
{       ITEM **my_items;
        int c, g, c_pos, s_y, s_x;                          
        MENU *my_menu;
        int n_choices, i;
        ITEM *cur_item;
        
        
        initscr();
        cbreak();
        noecho();
        keypad(stdscr, TRUE);
        
	getmaxyx(stdscr,s_y,s_x); /* Get screen size */
        
	mvprintw(5, 5, "x: %d",s_x);
	mvprintw(6, 5, "y: %d",s_y);

	n_choices = ARRAY_SIZE(choices);
        my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));

        for(i = 0; i < n_choices; ++i)
                my_items[i] = new_item(numbers[i],choices[i]);
        my_items[n_choices] = (ITEM *)NULL;

        my_menu = new_menu((ITEM **)my_items);

        post_menu(my_menu);
        refresh();
	
	c_pos=0;
        while( c_pos >= 0 )
        {   
        	c=getch();
        	switch(c){
                case KEY_DOWN:
                        menu_driver(my_menu, REQ_DOWN_ITEM);
                        if(c_pos < (n_choices-1)){
                        	c_pos++;
                        }
                        break;
                case KEY_UP:
                        menu_driver(my_menu, REQ_UP_ITEM);
                        if(c_pos != 0){
                        	c_pos--;
                        }        
                        break;
                }

		if(c==10){
			switch(c_pos){
			case 0:
				g=game(s_x, s_y);
				clear();
				break;
			case 4:
				c_pos=-1;
				break;
			}
		}
		refresh();
        }       

        free_item(my_items[0]);
        free_item(my_items[1]);
        free_menu(my_menu);
        endwin();
        return 0;
}

int do_touch(char moveto){
	return 0;
}

/* The game itself */
int game(int scr_x, int scr_y){
	int a = 0;
	int b = 0;
	int i;
	/* Player junk that will need to be moved when I add save/load */
	int p_x = 0;
	int p_y = 0;
	int scr_nlines = ARRAY_SIZE(screen);
	clear();
	while ( a >= 0 ){
		for(i = 0; i < scr_nlines; ++i){
			mvprintw(i,0,"%s",screen[i]);		
		}	
		mvprintw(p_y,p_x,"O");
		b=getch();
		switch(b){
			case KEY_UP:
				if(p_y > 0){
					mvprintw(p_y,p_x," ");
					p_y--;
				}
				break;
			case KEY_DOWN:
				if(p_y < (scr_y - 1 )){
					mvprintw(p_y,p_x," ");
					p_y++;
				}
				break;
			case KEY_RIGHT:
				if(p_x < (scr_x - 1)){
					mvprintw(p_y,p_x," ");
					p_x++;
				}
				break;
			case KEY_LEFT:
				if(p_x > 0){
					mvprintw(p_y,p_x," ");
					p_x--;
				}
				break;
		}
		refresh();
		//a = -1;
	}

	return 0;
}
